<?php

namespace common\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%customer_account}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $intergral
 * @property integer $intergral_balance
 */
class CustomerDues extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_dues}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'created_at','payment_id','status','site_id','mode'], 'integer'],
            [['money'],'number'],
            [['customer_id','money','code'],'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'id'),
            'customer_id' => Yii::t('common', '会员id'),
            'money' => Yii::t('common', '会费'),
            'payment_id' => Yii::t('common', '支付方式'),
            'status' => Yii::t('common', '状态'),
            'created_at' => Yii::t('common', '创建时间'),
            'code' => Yii::t('common', '订单编号'),
            'site_id'=>Yii::t('common','分店id'),
            'mode' => Yii::t('common', '所属类型（1共同体 2综合体 3管弦乐）'),
        ];
    }
    public function fields(){
        $fields = parent::fields();
        $fields['created_at'] = function(){
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }
    public function extraFields(){
        return [
            'customer_name'=>function(){
                return isset($this->customer)?$this->customer->name:'';
            },
            'pay_name' => function(){
                return isset($this->payment)?$this->payment->name:'';
            }
        ];
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }
    public function getPayment(){
        return $this->hasOne(Payment::className(),['id'=>'payment_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],

                ],
            ],

        ]);
    }
}
